package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.support.annotation.LayoutRes;
import android.view.View;

import com.android.databinding.library.baseAdapters.BR;

import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;

/**
 * Created by zxh on 16/12/11.
 */

public class RankItem extends BindingListItem {
    private static int[] rankImages;
    private int no;
    private int id;
    private String title;
    private String img;
    //    private String cover;
    private String videoPath;
    private String nickName;
    private String realName;
    private String hotDegree;
    private String desc;
    private String type;
    private int rankImage;

    static {
        rankImages = new int[5];
        rankImages[0] = R.mipmap.rank1;
        rankImages[1] = R.mipmap.rank2;
        rankImages[2] = R.mipmap.rank3;
        rankImages[3] = R.mipmap.rank4;
        rankImages[4] = R.mipmap.rank5;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
        if (no > 5 || no <= 0) {
            setRankImage(0);
        } else {
            setRankImage(rankImages[no - 1]);
        }
    }

    @Bindable
    public int getRankImage() {
        return rankImage;
    }

    public void setRankImage(int rankImage) {
        this.rankImage = rankImage;
        notifyPropertyChanged(BR.rankImage);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Bindable
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Bindable
    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }
//
//    @Bindable
//    public String getCover() {
//        return cover;
//    }
//
//    public void setCover(String cover) {
//        this.cover = cover;
//    }

    public String getVideoPath() {
        return videoPath;
    }

    public void setVideoPath(String videoPath) {
        this.videoPath = videoPath;
    }

    @Bindable
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Bindable
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Bindable
    public String getHotDegree() {
        return hotDegree;
    }

    public void setHotDegree(int hotDegree) {
        if (hotDegree == 0) {
            this.hotDegree = "";
        } else {
            String selectedType = "";
            switch (type) {
                case "w":
                case "v":
                    selectedType = "浏览";
                    break;
                case "e":
                    selectedType = "咨询";
                    break;
                default:
                    selectedType = "积分";
                    break;
            }
//            int result = (hotDegree / 10000);
            this.hotDegree = selectedType + String.valueOf(hotDegree);
        }
        notifyPropertyChanged(BR.hotDegree);
    }

    @Bindable
    public String getDesc() {
        return desc;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public RankItem(Context context, @LayoutRes int layoutId) {
        super(context, layoutId);
    }

    public void onItemClick(View view) {
        CommonFragment fragment = null;
        switch (type) {
            case "w":
                ArticleItem articleItem = new ArticleItem(getContext(), 0);
                articleItem.setArticleId(id);
                ArticleDetail articleDetail = new ArticleDetail(getContext(), articleItem);
                fragment = new CommonFragment();
                fragment.setVariableId(BR.detail);
                fragment.setLayoutId(R.layout.f_article_detail);
                fragment.setDataModel(articleDetail);
                HomeActivity.switchFragment((Activity) getContext(), fragment);
                break;
            case "v":
                // 打开视频详情
                final CommonFragment videoFragment = HomeActivity.openVideoDetail(getContext(),
                        id, null);
                LoadingDialog.getInstance(getContext()).show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            VideoDetail detail = WebApi.getVideoDetail((Activity) getContext(), id);
                            videoFragment.setDataModel(detail);
                        } catch (Exception ex) {

                        }
                        LoadingDialog.getInstance(getContext()).dismiss();
                    }
                }).start();
                break;
            case "e":
                HomeActivity.showPersonalCentral(getContext(), id, true);
                break;
            case "董秘":
            case "新人":
                HomeActivity.showPersonalCentral(getContext(), id, false);
                break;
        }
    }

    public void onClickAvatar(View view) {
        if (type == null)
            return;
        switch (type) {
            case "e":
            case "董秘":
            case "新人":
                HomeActivity.showPersonalCentral(getContext(), id, false);
                break;
        }
    }
}
